n,m = map(int, input().split())
nums = list(map(int, input().split()))
res = 0
while m > 0:
    s, e = 0, 0
    if not nums: break
    maxm = nums[0]
    cur = 0
    for i,x in enumerate(nums):
        cur = max(x, cur + x)
        if x > cur: s = i
        maxm = max(maxm, cur)
        if cur >= maxm: e = i
    res += maxm
    m -= 1
    nums = (nums[:s] if s > 0 else []) + (nums[e+1:] if e+1 < n else [])
print(res)